package com.hornsun.client.personalPage.api;

import com.hornsun.domain.dto.CouponRecordDTO;
import com.hornsun.client.personalPage.dto.MemberCouponDTO;
import com.hornsun.client.personalPage.service.CouponService;
import com.hornsun.repository.MemberCouponRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/24.
 */
@RestController
@RequestMapping("/api/coupon")
public class CouponAPI {
    @Autowired
    private CouponService couponService;

    @Autowired
    private MemberCouponRepository memberCouponRepository;

    @GetMapping("/couponRecord")
    public ResponseEntity<MemberCouponDTO> couponRecord(HttpServletRequest httpRequest,
                                                        @RequestParam("page")int page,
                                                        @RequestParam("type")int type){
        String openid = httpRequest.getAttribute("openid").toString();
        return new ResponseEntity<MemberCouponDTO>(couponService.getMemberCoupon(openid,page,type), HttpStatus.OK);
    }

    //当前订单可使用的优惠券
    @GetMapping("/useCoupon")
    public ResponseEntity<List<CouponRecordDTO>> useCoupon(HttpServletRequest httpRequest,@RequestParam("totalPrice")double totalPrice){
        String openid = httpRequest.getAttribute("openid").toString();
        List<CouponRecordDTO> couponRecordDTOList = new ArrayList<CouponRecordDTO>();
        if(totalPrice>=100){
            couponRecordDTOList.addAll(memberCouponRepository.queryUsableCouponByType(openid,(byte)0));
        }
        if (totalPrice>=200){
            couponRecordDTOList.addAll(memberCouponRepository.queryUsableCouponByType(openid,(byte)1));
        }
        return new ResponseEntity<List<CouponRecordDTO>>(couponRecordDTOList,HttpStatus.OK);
    }


}
